//
//  ForgetViewController.m
//  DBAChain
//
//  Created by Mxionlly on 2018/9/6.
//  Copyright © 2018年 Evan. All rights reserved.
//

#import "ForgetViewController.h"
#import "taskUserManagerService.h"

@interface ForgetViewController ()
@property (weak, nonatomic) IBOutlet UIButton *codeButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *passField;
@property (weak, nonatomic) IBOutlet UITextField *passField1;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (nonatomic, strong) NSTimer           *timer;
@property (nonatomic, assign) NSInteger         number;
///短信收到的验证码
@property (nonatomic, strong) NSString*         verifyCode;

@property (weak, nonatomic) IBOutlet UIButton *btnNewUser;

@property (weak, nonatomic) IBOutlet UIButton *btnOldUser;

@property (weak, nonatomic) IBOutlet UITextField *txtOldUserNo;

@property (weak, nonatomic) IBOutlet UILabel *labelOldLine;

@end

@implementation ForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"忘记密码";
    [self hideHeadView];
    [self initUI];
    [self setBlackStatus:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    self.hidesBottomBarWhenPushed = NO;
    self.tabBarController.tabBar.hidden = YES;
    if (self.navigationController.navigationBar.hidden == NO) {
        self.navigationController.navigationBar.hidden = YES;
    }
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)initUI{
    self.number = 60;
    
    [self.phoneField setValue:COLOR_main forKeyPath:@"_placeholderLabel.textColor"];
    [self.passField setValue:COLOR_main forKeyPath:@"_placeholderLabel.textColor"];
    [self.codeField setValue:COLOR_main forKeyPath:@"_placeholderLabel.textColor"];
    [self.passField1 setValue:COLOR_main forKeyPath:@"_placeholderLabel.textColor"];
}

- (IBAction)getCodeAction:(id)sender {
    if (![Check isMobileNumber:self.phoneField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }else{
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setField:self.phoneField.text value:@"phone"];
        [dict setField:@(0) value:@"uid"];
        [taskUserManagerService getVerifyCode:dict success:^(dataGetVerifyCode *response, BOOL success) {
            if (success) {
                self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updataAction) userInfo:nil repeats:YES];
                self.codeButton.userInteractionEnabled = NO;
                self.verifyCode = response.data;
            }else{
                [DKProgressHUD showErrorWithStatus:response.message];
            }
        } failture:^(NSError *error, NSString *strDescription) {
            [DKProgressHUD dismiss];
            [DKProgressHUD showErrorWithStatus:strDescription];
        }];
    }
}

- (void)updataAction {
    self.number--;
    [self.codeButton setTitle:[NSString stringWithFormat:@"%lds",(long)self.number] forState:UIControlStateNormal];
    if (self.number <= 0) {
        [self closeTimer];
    }
}

- (void)closeTimer {
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
        self.number = 60;
        self.codeButton.userInteractionEnabled = YES;
        [self.codeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    }
}

- (IBAction)forgetAction:(id)sender {
    if (![Check isMobileNumber:self.phoneField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    if (![Check checkLenght:self.codeField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    if (![Check checkLenght:self.passField.text] || ![Check checkLenght:self.passField1.text]) {
        [DKProgressHUD showErrorWithStatus:@"请设置密码"];
        return;
    }
    if (![self.passField.text isEqualToString:self.passField1.text]) {
        [DKProgressHUD showErrorWithStatus:@"两次密码设置不一致"];
        return;
    }
    if (self.codeField.text != self.verifyCode) {
        [DKProgressHUD showErrorWithStatus:@"您输入的验证码有误"];
        return;
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setField:self.phoneField.text value:@"phone"];
    [dict setField:self.passField.text value:@"newpwd"];
    [dict setField:self.codeField.text value:@"verifyCode"];
    [DKProgressHUD showLoading];
    [taskUserManagerService getForgetPassword:dict success:^(BaseModel *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            [DKProgressHUD showSuccessWithStatus:@"设置新密码成功"];
            [self goBack:nil];
        }else{
            [DKProgressHUD showErrorWithStatus:response.message];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
